﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CPPEI.ETL.DataServiceInterface;
using CPPEI.ETL.Domain.Services;
using CPPEI.WebApi;

namespace CPPEI.ETL.WebApi.DtSer
{
    [CPPEI.WebApi.AuthGetAction("RelationalDatabase", "GetRelationalDatabaseColumn", "1.0")]
    public class GetRelationalDatabaseColumnAction :
        BaseAction<GetRelationalDatabaseColumnParams, GetRelationalDatabaseColumnResult>
    {
        protected override ActionResult<GetRelationalDatabaseColumnResult> OnExecute(ActionContext context,
            SysParameters sysParams, GetRelationalDatabaseColumnParams opParams)
        {
            try
            {
                Dictionary<string, object> args = new Dictionary<string, object>();
                if (!string.IsNullOrEmpty(opParams.Parameters))
                    foreach (var item in opParams.Parameters.Split('&'))
                        if (!string.IsNullOrEmpty(item))
                        {
                            var temp = item.Split('=');
                            args.Add(temp[0], temp[1]);
                        }
                var _service = new RelationalDatabaseService();
                var result = _service.GetColumn(opParams.DataServiceId, opParams.SourceValue, opParams.Type, args);
                if (result != null && result.Any())
                    return new ActionResult<GetRelationalDatabaseColumnResult>(true, 0, "", new GetRelationalDatabaseColumnResult() { Columns = result });
                return new ActionResult<GetRelationalDatabaseColumnResult>(false, -1, "数据库返回结果为空", null);
            }
            catch (Exception ex)
            {
                return new ActionResult<GetRelationalDatabaseColumnResult>(false, -1, ex.Message, null);
            }
        }
    }
    public class GetRelationalDatabaseColumnParams
    {
        [Parameter]
        public Guid DataServiceId { get; set; }

        [Parameter]
        public string SourceValue { get; set; }

        [Parameter]
        public int Type { get; set; }

        [Parameter]
        public string Parameters { get; set; }
    }

    public class GetRelationalDatabaseColumnResult
    {
        public List<Column> Columns { get; set; }
        public GetRelationalDatabaseColumnResult()
        {
            Columns = new List<Column>();
        }
    }
}
